import { CreateModalList } from '@/biz-components/hoc-form/with-list';
import { onFieldValueChange } from '@formily/core';
import { message } from 'antd';
import { useRef, useState } from 'react';
import _ from 'lodash';
import { history } from "umi";
import api, { archivesBoxDelete } from './services';
import HistoryModal from './components/historyModal';

const WithListComp = CreateModalList({});

const FileBoxList = (props: any) => {
  const formRef = useRef();
  const { match } = props;
  const [refresh, setRefresh] = useState(false);
  const [viewCode, setViewCode] = useState('');
  const [viewCode1, setViewCode1] = useState('');
  const [visible, setVisible] = useState(false);
  const [visible1, setVisible1] = useState(false);

  return (
    <>
      <WithListComp
        match={match}
        history={history}
        reloadFlag={refresh}
        pageCode={'ArcvFileBox_L'}
        pageApi={api.page}
        noModal
        noCache
        formConfigs={{
          effects: (form: any) => {
            formRef.current = form;
            onFieldValueChange('belongCo', (field: any, form) => {
                form.setFieldState('whseCode', st => {
                  st.componentProps = _.merge(st.componentProps, {
                    disabled: !field.value,
                    api: {
                      params: {
                        queryKeys: {
                          belongCo: field.value,
                          pageCode: 'ArcvWhse_L'
                        }
                      }
                    }
                  })
                })
                if (!field.modified) return;
                form.setValuesIn('whseCode', undefined);
            })
          },
        }}
        events={{
          buttonsEvents: {
            handleCreate: () => {
              history.push('/archives/file-box/create');
            }
          },
          tableButtonsEvents: {
            handleEdit: (key: string, row: any) => {
                history.push(`/archives/file-box/edit/${row.id}`);
            },
            handleView: (key: string, row: any) => {
                history.push(`/archives/file-box/detail/${row.id}`)
            },
            handleViewOperations: (key: string, row: any) => {
                setViewCode(row.code);
                setVisible(true);
            },
            handlePrint: (key: string, row: any) => {
              window.open(`/arcv/arcvBox/printCover?code=${row.code}`);
            },
            handleViewHistory: (key: string, row: any) => {
              setViewCode1(row.code);
              setVisible1(true);
            },
            handleDelete: (key: string, row: any) => {
              archivesBoxDelete({
                code: row.code,
                pageCode: 'ArcvFileBox_L'
              }).then((res: any) => {
                if (res.success) {
                  message.success('删除成功');
                  setRefresh(true);
                }
              });
            }
          },
        }}
      />
      <HistoryModal
        title="操作记录"
        pageApi="/arcv/arcvBoxHis/page"
        queryKeys={{
          code: viewCode,
          pageCode: "ArcvFileBox_History_L"
        }}
        visible={visible}
        onCancel={() => { setVisible(false) }} />
      {/*todo*/}
      <HistoryModal
        title="借阅历史"

        pageApi="/arcv/arcvBrwr/page"
        queryKeys={{
          arcvBoxCode: viewCode1,
          pageCode: "ArcvFileBox_History_L"
        }}
        visible={visible1}
        onCancel={() => { setVisible1(false) }} />
    </>
  );
};

export default FileBoxList;
